package com.yzkj.cpjxc.business.service.impl;

import com.yzkj.cpjxc.business.framework.common.BusinessMap;
import com.yzkj.cpjxc.business.framework.common.Constants;
import com.yzkj.cpjxc.business.framework.common.Pager;
import com.yzkj.cpjxc.business.domain.Multimedia;
import com.yzkj.cpjxc.business.domain.MultimediaQueryVo;
import com.yzkj.cpjxc.business.service.MultimediaService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 多媒体文件 Service 实现
 */
@Service
public class MultimediaServiceImpl implements MultimediaService {

	/**
	 * MyBatis 命名空间
	 */
	private final static String MAPPER_NAMESPACE = "com.chkj.xezz.multimedia.mappers.MultimediaMapper";

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	@Override
	public BusinessMap create(Multimedia multimedia) throws Exception {
		BusinessMap bm = new BusinessMap();
		//if (this.isExists(multimedia)) {
		//	bm.put(Constants.WARNING_MSG, "该多媒体文件名称已经存在");
		//	return bm;
		//}
		Date date = new Date();
		multimedia.setCreateTime(date);
		multimedia.setUpdateTime(date);
		multimedia.setIsActive(Constants.ACTIVE);
		
		sqlSessionTemplate.insert(MAPPER_NAMESPACE + ".add", multimedia);
		return bm;
	}

	@Override
	public boolean isExists(Multimedia multimedia) throws Exception {
		return (Integer)sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".isExists", multimedia) > 0;
	}
	
	@Override
	public Multimedia getById(String uuid) throws Exception {
		if(StringUtils.isBlank(uuid)){
			return null;
		}
		return sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getById", uuid);
	}
	
	@Override
	public List<Multimedia> getByFkUuid(String fkUuid) throws Exception {
		if(StringUtils.isBlank(fkUuid)){
			return null;
		}
		return sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".getByFkUuid", fkUuid);
	}
	
	@Override
	public BusinessMap edit(Multimedia multimedia) throws Exception {
		BusinessMap bm = new BusinessMap();
		//if (this.isExists(multimedia)) {
		//	bm.put(Constants.WARNING_MSG, "该多媒体文件名称已经存在");
		//	return bm;
		//}
		Date date = new Date();
		multimedia.setUpdateTime(date);
		
		sqlSessionTemplate.update(MAPPER_NAMESPACE + ".edit", multimedia);
		return bm;
	}

	@Override
	public boolean falseDelete(String uuid) throws Exception {
		
		Multimedia m = new Multimedia();
		m.setUuid(uuid);
		m.setUpdateTime(new Date());
		
		sqlSessionTemplate.delete(MAPPER_NAMESPACE + ".falseDeleteById", m);
		return true;
	}
	
	@Override
	public boolean delete(String uuid) throws Exception {
		sqlSessionTemplate.delete(MAPPER_NAMESPACE + ".deleteById", uuid);
		return true;
	}

	@Override
	public Pager<Multimedia> search(Pager<Multimedia> pager, MultimediaQueryVo queryVo) throws Exception {
		List<Multimedia> dataList = sqlSessionTemplate.selectList(MAPPER_NAMESPACE + ".page", queryVo, new RowBounds((pager.getCurrentPage() - 1) * pager.getLimit(), pager.getLimit()));
		pager.setDataList(dataList);
		
		// 如果需要转换成ShowVo展示，那么就在这里转换
		
		Long total = sqlSessionTemplate.selectOne(MAPPER_NAMESPACE + ".getTotal", queryVo);
		pager.setTotal(total);
		return pager;
	}

}
